package com.itfenghuang.day08;

import com.itfenghuang.day07.jdbc_atm;


import java.sql.*;
import java.util.Scanner;
@SuppressWarnings("all")
public class Atm {
    public static Scanner sc = new Scanner(System.in);
    public static jdbc_atm jdbc=new jdbc_atm();

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        System.out.println("=========================================================");
        System.out.println("================欢迎光临来到凤凰取款机=====================");
        System.out.println("==========================================================");

        while(true){
            System.out.println("1. 登录   2. 注册   3. 忘记密码  4. 挂失   5.解锁    6.退出");
            System.out.println("请选择需要办理事项:");
            String i = sc.nextLine();

            if("1".equals(i)){
                System.out.println("选择的登录,调用登录模块");
                String name = login();  // 返回当前登录成功了的用户名

                if( !("0".equals(name)) ){
                    System.out.println("登录成功,进入二级界面");
                    while (true){
                        System.out.println(" 1. 查询余额  2. 存钱  3. 取钱  4. 转账  5. 修改密码  6. 查询个人信息以及vip等级  7.返回上一级  8退出系统");
                        System.out.println("请选择操作:");
                        String j = sc.nextLine();
                        if("1".equals(j)){
                            System.out.println("查询余额");
                            showMoney(name);
                            break;
                        }else if("2".equals(j)){
                            System.out.println("存钱");
                            setMoney( name);
                            break;
                        }else if("3".equals(j)){
                            System.out.println("取钱");
                            getMoney(name);
                            break;
                        }else if("4".equals(j)){
                            System.out.println("转账");
                            changeMoney(name);
                            break;
                        }else if("5".equals(j)){
                            System.out.println("修改密码");
                            changePassword(name);
                            break;
                        }else if("6".equals(j)){
                            System.out.println("查询个人信息以及vip等级");
                        }else if("7".equals(j)){
                            System.out.println("返回上一级");
                            break;
                        }else if("8".equals(j)){
                            System.out.println("退出系统");
                            return;
                        }else {
                            System.out.println("又在瞎搞了");
                        }
                    }
                }
            }else if("2".equals(i)){
                System.out.println("注册");
                register();
            }else if("3".equals(i)){
                System.out.println("忘记密码");
                 forgetpassword();
            }else if("4".equals(i)){
                System.out.println("挂失");
            }else if("5".equals(i)){
                System.out.println("解锁");
            }else if("6".equals(i)){
                System.out.println("退出");
                return;
            }else {
                System.out.println("不要瞎xxx搞");
            }
        }
    }
//    登陆
    public static String login() throws SQLException {
        while (true){
            System.out.println("开始登陆,请输入姓名");
            String name = sc.nextLine();
            System.out.println("请输入密码");
            String password = sc.nextLine();

            String sql = "select password  from  atm  where name='" + name + " '";
            ResultSet result = jdbc.select(sql);
            if (result.next()){
                System.out.println("用户名存在,开始比较密码");
                String password1 = result.getString("password");
                System.out.println(password1 + " : " + password);
                if (password.equals(password1)){
                    return name;
                }
            }
        }
    }

//    注册
    public static void register() throws SQLException {
        while(true) {
            System.out.println("请输入用户名");
            String name = sc.nextLine();

            String sql="select money from atm where name='"+name+"'";
            ResultSet result = jdbc.select(sql);

            if (result.next()) {
                System.out.println("用户名已存在,请重新输入");
            } else {
                System.out.println("请输入密码");
                String password = sc.nextLine();

                String sql1 = "insert into atm(name,password) values('" + name + "','" + password + "')";
                int update = jdbc.update(sql1);
                System.out.println("注册成功");
                break;
            }
        }


    }

//    忘记密码
    public static void forgetpassword() throws SQLException {
        System.out.println("请输入姓名");
        String name = sc.nextLine();

        String sql="select  id_car,name  from atm  where  name='"+name+"'";
        ResultSet result = jdbc.select(sql);
        if (result.next()){
            String name1 = result.getString("name");
            System.out.println(name+" ; "+name1);

            if(name.equals(name1)){
                System.out.println("用户名正确,请输入id");
                int id = sc.nextInt();

                String id_car = result.getString("id_car");
                System.out.println(id+" ; "+id_car);
                System.out.println("id正确");

                System.out.println("开始修改");
                String password=sc.nextLine();
                String sql1="update atm set password="+password+" where name='"+name+"'";
                int update = jdbc.update(sql);

            }
        }


    }
    //修改密码
    public static void changePassword(String name) throws SQLException {
        System.out.println("开始修改");
        String password=sc.nextLine();

        String sql="update atm set password="+password+" where name='"+name+"'";
        int update = jdbc.update(sql);
        System.out.println("修改成功");
    }

    //查询
    public static void showMoney(String name) throws SQLException {

        String sql="select money from atm where name='"+name+"'";
        ResultSet result = jdbc.select(sql);

        if(result.next()){
            System.out.println(result.getInt("money"));
        }
        System.out.println("查询成功");
    }

    // 存钱
    public static void setMoney(String name) throws SQLException {
        System.out.println("请填写存储金额");
        int money = sc.nextInt();

        String sql = "update  atm  set  money=money + "+money+" where name='" + name + "'";
        int result = jdbc.update(sql);
        System.out.println("存储成功");
    }

    // 取钱
    public static void getMoney(String name) throws SQLException {
        System.out.println("填写取款金额");
        int money = sc.nextInt();

        String sql = "update  atm  set  money=money - "+money+" where name='" + name + "'";
        int result = jdbc.update(sql);


        System.out.println("操作成功");
    }

    // 转账
    public static void changeMoney(String name) throws SQLException {
        while (true){
            System.out.println("输入转账对象");
            String name1 = sc.nextLine();
            String sql = "select money from atm  where name='" + name + "'";
            ResultSet result = jdbc.select(sql);
            if(result.next()){
                int money = result.getInt("money");
                System.out.println("账户余额:" + money);
                if(money<100){
                    System.out.println("余额不足,请充值");
                    break;
                }
                System.out.println("请输入转账金额");
                int i = sc.nextInt();
                String sql1 = "update  atm  set  money=money - "+i+" where name='" + name + "'";
                int update = jdbc.update(sql1);
                System.out.println("转账成功");

            }
        }



    }





}
